//
//  PopoverAnimateNSObject.swift
//  微博
//
//  Created by 段瑞金 on 16/5/12.
//  Copyright © 2016年 段瑞金. All rights reserved.
//

import UIKit

class PopoverAnimateNSObject: NSObject {
    //定义一个属性,保存当前的标题控制器是弹出还是消失
    private var onAnimate = false
}
//MARK:-转场代理方法

extension PopoverAnimateNSObject : UIViewControllerTransitioningDelegate{
    
    /// 该代理方法用于返回负责转场的控制器对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        
        return PopOverViewController(presentedViewController: presented, presentingViewController: presenting)
    }
    
    /// 该代理方法用于告诉系统谁来负责控制器如何弹出
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        //自定义动画弹出时,会调用这个方法
        onAnimate = true
        
        return self
    }
    /// 该代理方法用于告诉系统谁来负责控制器如何消失
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        //自定义动画消失时,会调用这个方法
        onAnimate = false
        return self
        
    }
    
}



extension PopoverAnimateNSObject : UIViewControllerAnimatedTransitioning {
    
    //动画时间,但是默认是用不上的,所以可以任意返回一个值
    //返回的时间,主要是在自定义动画的时候,弹出和消失的时间一至,方便管理,
    //只需要在执行弹出和消失的时候,调用这个方法,就会返回一个时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
    {
        return 0.3
    }
    
    
    
    // 系统调用该方法时会传递一个transitionContext参数, 该参数中包含了我们所有需要的值
    func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        //在做动画的时候,先判断一下,是弹出还是消失
        if onAnimate {//弹出
            
            animateTransitionPresentedController(transitionContext)
            
        }else{//消失
            animateTransitionDismissedController(transitionContext)
        }
    }
    
    
    //自定义弹出方法
    private func animateTransitionPresentedController(transitionContext: UIViewControllerContextTransitioning?) -> (){
        
        
        //动画时间
        let duration = transitionDuration(transitionContext)
        
        guard let toView = transitionContext!.viewForKey(UITransitionContextToViewKey) else {
            return
        }
        // 2.将被弹出的控制器View添加到容器视图上
        transitionContext!.containerView()?.addSubview(toView)
        // 3.执行动画
        // 3.1先将菜单的View压扁
        toView.transform = CGAffineTransformMakeScale(1.0, 0)
        //修改锚点
        toView.layer.anchorPoint = CGPointMake(0.5, 0)
        //执行动画
        UIView.animateWithDuration(duration, animations: { () -> Void in
            //
            toView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                // 注意点: 但凡是自定义转场, 一定要在自定义动画完成之后告诉系统动画已经完成了, 否则会出现一些未知异常
                transitionContext!.completeTransition(true)
        }
        
    }
    
    //自定义消失方法
    private func animateTransitionDismissedController(transitionContext: UIViewControllerContextTransitioning?) -> (){
        
        
        //动画时间
        let duration = transitionDuration(transitionContext)
        //获取控制器
        guard let fromView = transitionContext!.viewForKey(UITransitionContextFromViewKey) else {
            return
        }
        
        //执行动画
        UIView.animateWithDuration(duration, animations: { () -> Void in
            //MakeScale(1.0, 0.00001)里面的接收的是cgfloat类型,所以如果给y值传递一个0的话,不行,所以传递一个比较小的值即可
            fromView.transform = CGAffineTransformMakeScale(1.0, 0.00001)
            }) { (_) -> Void in
                transitionContext!.completeTransition(true)
        }
    }
    
}
